<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\db\Query;
use think\Session;

class Talk extends Controller
{


    public function index($id)
    {
//        halt($id);

        $list=Db::table('wb_talk_message e')
            ->join('wb_message m','e.mid=m.id','left')
            ->join('wb_mpicture p','p.mid=m.id','left')
            ->join('wb_comments c','c.mid=m.id','left')
            ->join('wb_buser b','m.uid=b.id','left')
            ->join('wb_buserinfo i','b.id=i.uid','left')
            ->field(['c.content','b.id uid','m.agreenum','group_concat(p.path) path','m.content','m.transpondnum','m.readnum','m.commentnum','b.username','m.time','m.id','i.photo'])
            ->where('e.tid='.$id)
            ->group('e.tid')
            ->group('m.id')
            ->select();
//        halt($list);
        // 话题名称
        $n=Db::table('wb_talk')
            ->field('name')
            ->where('id='.$id)
            ->find();
        $name = implode("",$n);
//        halt($name);
        $uid = Session::get('id');
//        halt($uid);
        if ($uid){
            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $uid)
                ->find();
            $photo = implode("", $p);
            $username = Session::get('username');
            return view('talk/index',[
                'id' => $uid,
                'list'=>$list,
                'name'=>$name,
                'photo'=>$photo,
                'username'=>$username
            ]);
        }else{
            return view('talk/index',[
                'list'=>$list,
                'name'=>$name
            ]);
        }

    }


    public function name()
    {
        $nam = request()->get('name');
        $name = str_replace('\'','',$nam);
//        halt($nam);

        $list=Db::table('wb_talk_message e')
            ->join('wb_message m','e.mid=m.id','left')
            ->join('wb_mpicture p','p.mid=m.id','left')
            ->join('wb_comments c','c.mid=m.id','left')
            ->join('wb_buser b','m.uid=b.id','left')
            ->join('wb_talk t','t.id=e.tid')
            ->join('wb_buserinfo i','i.uid=b.id')
            ->field(['c.content','b.id uid','m.agreenum','group_concat(p.path) path','m.content','m.transpondnum','m.readnum','m.commentnum','b.username','m.time','m.id','photo'])
            ->where('t.name ='.$nam)
            ->group('e.tid')
            ->group('m.id')
            ->select();
//        halt($list);
        $id = Session::get('id');
        if (empty($id)) {
            return view('talk/index',[
                'list'=>$list,
                'name'=>$name,
                'id' => $id,

            ]);
        }else{
            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $id)
                ->find();
            $photo = implode("", $p);
            $username = Session::get('username');
            return view('talk/index',[
                'list'=>$list,
                'name'=>$name,
                'id' => $id,
                'photo'=>$photo,
                'username'=>$username

            ]);
        }
        
    }

}

